#!/bin/bash

echo "🚀 Offline Gathering Backend - Quick Start"
echo "========================================="

# Check if Node.js is installed
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js first."
    echo "   Visit: https://nodejs.org/"
    exit 1
fi

# Check if MongoDB is running
if ! command -v mongo &> /dev/null && ! command -v mongod &> /dev/null; then
    echo "⚠️  MongoDB command line tools not found."
    echo "   Make sure MongoDB is installed and running."
fi

echo "✅ Node.js version: $(node --version)"
echo "✅ NPM version: $(npm --version)"

# Install dependencies
echo "📦 Installing dependencies..."
npm install

if [ $? -ne 0 ]; then
    echo "❌ Failed to install dependencies"
    exit 1
fi

# Create uploads directory if it doesn't exist
if [ ! -d "uploads" ]; then
    echo "📁 Creating uploads directory..."
    mkdir uploads
fi

# Check if .env file exists
if [ ! -f ".env" ]; then
    echo "⚠️  .env file not found. Creating from .env.example..."
    cp .env.example .env
    echo "⚠️  Please edit .env file with your configuration"
fi

echo ""
echo "🎯 Quick Start Options:"
echo "1. Start development server with nodemon"
echo "2. Start production server"
echo "3. Seed database with sample data"
echo "4. Run all (seed + dev server)"
echo "5. Exit"
echo ""

read -p "Select an option (1-5): " choice

case $choice in
    1)
        echo "🚀 Starting development server..."
        npm run dev
        ;;
    2)
        echo "🚀 Starting production server..."
        npm start
        ;;
    3)
        echo "🌱 Seeding database..."
        npm run seed
        ;;
    4)
        echo "🌱 Seeding database..."
        npm run seed
        echo "⏳ Waiting for database seeding to complete..."
        sleep 3
        echo "🚀 Starting development server..."
        npm run dev
        ;;
    5)
        echo "👋 Goodbye!"
        exit 0
        ;;
    *)
        echo "❌ Invalid option. Exiting..."
        exit 1
        ;;
esac